// pages/userCenter/registuser.js
var rsa = require('../../utils/rsa.js');
var mdk = require('../../utils/md55.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mer_name:'',
    log_name:'',
    tel_num:'',
    regist_code:'',
    password:'',
    repassw:'',
    showverification:'获取验证码',
    showtime: '',
    count: 60,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  //获取验证码
  getphonecode:function(){
    const telNum = this.data.tel_num;
    if(!telNum) return;
    // const pot = "http://192.168.2.155:11114/pos/basicSend/basic_getShortMessage";
    rsa.httpPost('basic_getShortMessage', {"phone":telNum},(err,res)=>{
      if(err){
        wx.showToast({
          title: err,
          icon:'none',
          duration:2000,
        })
        return;
      }
      var recode = res.return_code;
      if (recode == '0000' ){
        wx.showToast({
          title: '发送成功',
          icon: 'success',
          duration: 3000
        })
      }else{
        wx.showToast({
          title: res.return_msg,
          icon:'none',
          duration:3000,
        })
      }
      
    })
    this.beginTimer();
  },
  // 启动倒计时
  beginTimer() {
    var Timers = null;
    this.setData({
      showverification: '',
      count: 60,
      showtime: '60s'
    })
    var that = this;
    //如果定时器已经器动了
    if (!Timers) {
      Timers = setInterval(function () {
        var count = that.data.count - 1;
        that.setData({
          showtime:  count + "s",
          Timers: Timers,
          count: count
        })
        if (count <= 0) {
          clearInterval(Timers)
          that.setData({
            count: 60,
            showverification: '获取验证码',
            showtime: ''
          })
        }
      }, 1000);
    }
  },
  //注册
  registAction:function(){
    const mername = this.data.mer_name;
    const loginname = this.data.log_name;
    const telNum = this.data.tel_num;
    const registcode = this.data.regist_code;
    const pass = this.data.password;
  
    var shownoto = '';
    if(!mername){
      shownoto = '商户名不为空';
    }else if(!telNum){
      shownoto = "手机号不为空";
    } else if (!registcode){
      shownoto='验证码不为空';
    }else if(!pass){
      shownoto = '密码不为空';
    }else{

    }
    if(shownoto){
      wx.showToast({
        title: shownoto,
        icon:'none',
        duration:3000,
      })
      return
    }
    var password = mdk.hexMD5(pass);
    // const pot = 'http://192.168.2.155:11114/pos/basicSend/basic_add';
    rsa.httpPost('basic_add', { "code": registcode, "userPassWord": password, "phone": telNum, "merName": mername, "promoCode": loginname}, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000,
        })
        return;
      }
     wx.showModal({
       title: '提示',
       content: '注册成功，是否返回登录界面',
       success: function (res) {
         if (res.confirm) {
           wx.navigateBack({
             delta:1,
           })
         } else if (res.cancel) {
          
         }
       }
     })
    })




  },
  phoneNum:function(e){
    this.data.tel_num = e.detail.value;
  },
  merName:function(e){
    this.data.mer_name = e.detail.value;
  },
  loginUser:function(e){
    this.data.log_name = e.detail.value;
  },
  registCode:function(e){
    this.data.regist_code =e.detail.value;
  },
  passwordAction:function(e){
    this.data.password = e.detail.value;
  },
  rePassAction:function(e){
    this.data.repassw = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})